package com.autolinq.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.autolinq.util.http.HttpUtil;



/**
 * 
 * @author 简爱微萌
 * @Email zyw205@gmail.com
 * 接口权限中设置OAuth2.0网页授权 域名 如：www.wechat68.com
 * 授权访问的URL：https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx614c453e0d1dcd12&redirect_uri=http://www.wechat68.com/Javen/OauthTest&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect
 */
public class Oauth2TestServlet extends HttpServlet {
private String get_access_token_url="https://api.weixin.qq.com/sns/oauth2/access_token?" +
        "appid=APPID" +
        "&secret=SECRET&" +
        "code=CODE&grant_type=authorization_code";
private String get_userinfo="https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";

    private static final long serialVersionUID = -644518508267758016L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        // 将请求、响应的编码均设置为UTF-8（防止中文乱码）  
        request.setCharacterEncoding("UTF-8");  
        response.setCharacterEncoding("UTF-8"); 
        String code=request.getParameter("code");
        

        String json=HttpUtil.getUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3093e3f2805b334a&redirect_uri=http://t.versalinks.com/autoLinQ/oauthServlet&response_type=code&scope=snsapi_base");
        System.out.println("json="+json);
        
        
        
    //    UserInfo_weixin userInfo=new UserInfo_weixin(user_openid, user_nickname, user_sex, user_province, user_city, user_country, user_headimgurl);
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(json);

        out.println();
        out.flush();
        out.close();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("");
        out.println("");
        out.println("  ");
        out.println("  ");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the POST method");
        out.println("  ");
        out.println("");
        out.flush();
        out.close();
    }


}